package com.example.recruitment_website.service.Impl;

import com.example.recruitment_website.domain.Article;
import com.example.recruitment_website.domain.Collection;
import com.example.recruitment_website.domain.Record;
import com.example.recruitment_website.repository.ArticleRepository;
import com.example.recruitment_website.repository.RecordRepository;
import com.example.recruitment_website.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RecordImpl implements RecordService {

    @Autowired
    ArticleRepository articleRepository;
    @Autowired
    RecordRepository recordRepository;

    @Override
    public List<Record> findAllRecordById(int id) {

        List<Record> list = recordRepository.findByUser_id(id);
        return list;
    }

    @Override
    public Void addRecord(int id, int userId) {

        Article article = articleRepository.findById(id);
        if (article == null){
            return null;
        }
        recordRepository.deleteByArticle_idAndUser_id(id,userId);
//        Record record1 = recordRepository.findByIdAndArticle_id(id,userId);
//        if (record1 == null){
            Record record = new Record(userId,id,article.getTitle(),article.getImg_url(),article.getDate());
            recordRepository.save(record);
//        }

        return null;
    }
}
